<?php
namespace Mouta\AmistateBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Mouta\AmistateBundle\utils\Strings;
use Mouta\AmistateBundle\utils\Thumbnail;

/**
 * Mouta\AmistateBundle\Entity
 * 
 * @ORM\Table(name="imagen_usuario")
 * @ORM\Entity(repositoryClass="Mouta\AmistateBundle\Entity\ImagenUsuarioRepository")
 */
class ImagenUsuario
{
    CONST MAXIMAGES =10;
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    protected $id;

    /**
     * @ORM\ManyToOne(targetEntity="Usuario")
     * @ORM\JoinColumn(name="usuario_id", referencedColumnName="id", onDelete="Cascade")
     */
    protected $usuario;

    /**
     * @ORM\Column(type="string", nullable=true)
     */
    protected $label;

    /**
     * @ORM\Column(type="string")
     * @Assert\NotBlank()
     */
    protected $imagen;

    /**
     * @ORM\Column(type="string")
     * @Assert\NotBlank()
     */
    protected $imagen_big;

    /**
     * @ORM\Column(type="integer",length="1")
     */
    protected $defa = 0;

    /**
     * @ORM\Column(type="integer",length="1")
     */
    protected $manual = 1;

    /**
     * @ORM\Column(type="datetime")
     */
    protected $created_at = 1;


    public function uploadImagen($request){
        if (null === $request->files) {
            return false;
        }

        $foto = $request->files->get('foto');
        if($foto === null)
            return false;
        $aux = explode('/',$foto->getMimeType());
        $ext = end($aux);
        $nameSinExtension = time();
        $fileName = $nameSinExtension.'.'.$ext;
        $foto->move(
    	        $this->getPathTempImage(),
    	        $fileName
    	    );
        $errorT = false;
        $thumbnail = new Thumbnail($this->getPathTempImage().$fileName);
        $thumbnail->Thumbnail($this->getPathTempImage().$fileName);
        if(empty ($thumbnail->error_msg))
        {
            $thumbnail->size_auto(90);
            $thumbnail->process();
        }else{
            $errorT = true;
        }
        
        $bigThumbnail = new Thumbnail($this->getPathTempImage().$fileName);
        $bigThumbnail->Thumbnail($this->getPathTempImage().$fileName);
        if(empty ($bigThumbnail->error_msg))
        {
            $bigThumbnail->size_auto(600);
            $bigThumbnail->process();
        }else{
            $errorT = true;
        }
        
        unlink($this->getPathTempImage().$fileName);
        $fileName = $nameSinExtension . '.' . strtolower($thumbnail->img["format"]);
        if(!$errorT)
        {
            $thumbnail->save($this->getPathImage().'t-'.$fileName);
            $bigThumbnail->save($this->getPathImage().$fileName);
            
            $this->setImagen('t-'.$fileName);
            $this->setImagenBig($fileName);
            return true;
        }else{
            return false;
        }
                                        
        
    }
    public function getPathImage(){
        $usuario_id = $this->getUsuario()->getId();
        $folder = ceil($usuario_id / 1000);
        $dir = $_SERVER['DOCUMENT_ROOT']."/upload/".$folder.'/'.$usuario_id.'/';
        if( !is_dir($dir) )
            mkdir($dir,0777,true);
        return $dir;
    }
    public function getPathTempImage(){
        $dir = $_SERVER['DOCUMENT_ROOT']."/upload/tmp/";
        if( !is_dir($dir) )
            mkdir($dir,0777,true);
        return $dir;
    }
    public function getPathWeb($grande = false){
        if($this->imagen_big === false)
        {
            if($this->getUsuario()->getSexo())
                return '/upload/default/mujer.png';
            else
                return '/upload/default/hombre.png';
        }
            
        $usuario_id = $this->getUsuario()->getId();
        $folder = ceil($usuario_id / 1000);
        $dir = "/upload/".$folder.'/'.$usuario_id.'/';
        if($grande)
            return $dir.$this->imagen_big;
        else
            return $dir.$this->imagen;
    }
    
    public function __toString(){
        return $this->getPathWeb();
    }


    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set label
     *
     * @param string $label
     */
    public function setLabel($label)
    {
        $this->label = $label;
    }

    /**
     * Get label
     *
     * @return string 
     */
    public function getLabel()
    {
        return $this->label;
    }

    /**
     * Set imagen
     *
     * @param string $imagen
     */
    public function setImagen($imagen)
    {
        $this->imagen = $imagen;
    }

    /**
     * Get imagen
     *
     * @return string 
     */
    public function getImagen()
    {
        return $this->imagen;
    }

    /**
     * Set imagen_big
     *
     * @param string $imagenBig
     */
    public function setImagenBig($imagenBig)
    {
        $this->imagen_big = $imagenBig;
    }

    /**
     * Get imagen_big
     *
     * @return string 
     */
    public function getImagenBig()
    {
        return $this->imagen_big;
    }

    /**
     * Set defa
     *
     * @param integer $defa
     */
    public function setDefa($defa)
    {
        $this->defa = $defa;
    }

    /**
     * Get defa
     *
     * @return integer 
     */
    public function getDefa()
    {
        return $this->defa;
    }

    /**
     * Set manual
     *
     * @param integer $manual
     */
    public function setManual($manual)
    {
        $this->manual = $manual;
    }

    /**
     * Get manual
     *
     * @return integer 
     */
    public function getManual()
    {
        return $this->manual;
    }

    /**
     * Set usuario
     *
     * @param Mouta\AmistateBundle\Entity\Usuario $usuario
     */
    public function setUsuario(\Mouta\AmistateBundle\Entity\Usuario $usuario)
    {
        $this->usuario = $usuario;
    }

    /**
     * Get usuario
     *
     * @return Mouta\AmistateBundle\Entity\Usuario 
     */
    public function getUsuario()
    {
        return $this->usuario;
    }

    /**
     * Set created_at
     *
     * @param datetime $createdAt
     */
    public function setCreatedAt($createdAt)
    {
        $this->created_at = $createdAt;
    }

    /**
     * Get created_at
     *
     * @return datetime 
     */
    public function getCreatedAt()
    {
        return $this->created_at;
    }
}